package TryCode.TCP.multy;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class client1 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 5000);

            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

            // 创建一个线程用于接收服务器的消息
            Thread receiverThread = new Thread(() -> {
                try {
                    String message;
                    while ((message = reader.readLine()) != null) {
                        System.out.println("收到服务器消息: " + message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            receiverThread.start();

            // 从控制台读取用户输入并发送给服务器
            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
            String message;
            while ((message = userInput.readLine()) != null) {
                writer.println(message);
                writer.flush();
            }

            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
